<template>
  <div class="box">
    <el-popover
      ref="popoverRef"
      placement="bottom-start"
      :width="300"
      trigger="click"
      :teleported="false"
      :visible="fillterShow"
    >
      <template #reference>
        <div @click="clickShowFilter" v-click-outside="handleClickOutside" :disabled="loading" class="filter-button">
          <span>筛选</span>
          <el-icon><Filter /></el-icon>
        </div>
      </template>
      <el-form label-position="top" label-width="auto" :model="filterForm" class="query-box" :inline="true">
        <el-form-item label="选择">
          <el-select :teleported="false" v-model="filterForm.selectVal" placeholder="请选择">
            <el-option :key="0" label="全部" value="0" />
            <el-option :key="1" label="库存" :value="1" />
            <el-option :key="2" label="清仓" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="查询">
          <el-input v-model="filterForm.inputVal" placeholder="请输入" suffix-icon="Search" />
        </el-form-item>
        <el-form-item label="单选框">
          <el-radio-group v-model="filterForm.radioVal">
            <el-radio value="1">金属</el-radio>
            <el-radio value="2">塑料</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="日期选择">
          <el-date-picker :teleported="false" v-model="filterForm.dateVal" type="date" placeholder="日期选择" />
        </el-form-item>
        <div class="form-button">
          <el-button :loading="loading" @click="handleCancel()">重置</el-button>
          <el-button :loading="loading" type="primary" @click="handleConfirm">确定</el-button>
        </div>
      </el-form>
    </el-popover>
  </div>
</template>
<script setup lang="ts">
import { ClickOutside as vClickOutside } from 'element-plus';

const loading = ref<boolean>(false);

interface filterParams {
  selectVal: string;
  inputVal: string;
  radioVal: string;
  dateVal: string;
}
const filterForm = ref<filterParams>({
  selectVal: '0',
  inputVal: '',
  radioVal: '1',
  dateVal: ''
});

const popoverRef = ref<any>(null);
// 筛选弹出框开关
const fillterShow = ref<boolean>(false);

// 关闭弹窗
const clickShowFilter = async () => {
  fillterShow.value = !fillterShow.value;

  if (fillterShow.value) {
    // 判断之前是否有筛选过的参数，有就把该参数赋值给filterForm.value，没有就初始化（相当于重置）
    if (sessionStorage.getItem('filterFormQuery')) {
      Object.assign(filterForm.value, JSON.parse(sessionStorage.getItem(`filterFormQuery`) || ''));
    } else {
      handleCancel('init');
    }
  }
};

// 重置
const handleCancel = async (type = '') => {
  filterForm.value = {
    selectVal: '0',
    inputVal: '',
    radioVal: '1',
    dateVal: ''
  };
  if (type === 'init') {
    return false;
  }
  // 清除筛选参数
  sessionStorage.removeItem('filterFormQuery');

  // 调用接口
};

// 确定
const handleConfirm = async () => {
  // 关闭弹窗
  fillterShow.value = false;

  // 保存筛选参数
  sessionStorage.setItem('filterFormQuery', JSON.stringify(filterForm.value));
  // 调用接口
};

const handleClickOutside = (event: { target: any }) => {
  const popoverContent = popoverRef.value?.popperRef?.contentRef;
  if (fillterShow.value && !popoverContent?.contains(event.target)) {
    fillterShow.value = false;
  }
};
</script>

<style lang="scss" scoped>
.box {
  margin: 16px;
  // background-color: #fff;
  overflow: auto;
  height: 100%;
  padding: 16px;
  box-sizing: border-box;

  .filter-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 64px;
    height: 32px;
    line-height: 32px;
    font-size: 14px;
    font-weight: 400;
    color: var(--el-color-blue);
    cursor: pointer;
    border: 1px solid var(--el-border-color-base);
    border-radius: var(--el-border-radius-base);

    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }

  .query-box {
    :deep(.el-input) {
      width: 220px;
    }
    :deep(.el-select) {
      width: 220px;
    }
  }

  .form-button {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    width: 100%;
  }
}
</style>
